package com.eightbears.beboy.base.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.eightbears.beboy.R;


/**
 * Created by Bao on 2016/12/20.
 */

public class ViewVipTitleBar extends RelativeLayout {


    private LinearLayout ll_back;
    private ImageView iv_vip_bg;
    private ImageView iv_user_head;
    private TextView tv_user_nickname;
    private TextView tv_date;

    private Context context;
    private ImageView mIv_level;
    private ImageView mIv_vip;

    public ViewVipTitleBar(Context context) {
        super(context);
        init(context, null);
    }

    public ViewVipTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(context, attrs);
    }

    public ViewVipTitleBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.view_by_vip_title, this);
        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        iv_vip_bg = (ImageView) findViewById(R.id.iv_vip_bg);
        iv_user_head = (ImageView) findViewById(R.id.iv_user_head);
        tv_user_nickname = (TextView) findViewById(R.id.tv_user_nickname);
        tv_date = (TextView) findViewById(R.id.tv_date);
        mIv_level = (ImageView) findViewById(R.id.iv_level);
        mIv_vip = (ImageView) findViewById(R.id.iv_vip);

        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ByTitleBar);
            String title = ta.getString(R.styleable.ByTitleBar_titleBarUserNickname);
            tv_user_nickname.setText(title);
            String date = ta.getString(R.styleable.ByTitleBar_titleBarVipDate);
            tv_date.setText(date);

            Drawable bgDrawable = ta.getDrawable(R.styleable.ByTitleBar_titleBarVipTypeBg);
            if (null != bgDrawable) {
                iv_vip_bg.setImageDrawable(bgDrawable);
            }
            Drawable headDrawable = ta.getDrawable(R.styleable.ByTitleBar_titleBarUserHead);
            if (null != headDrawable) {
                iv_user_head.setImageDrawable(headDrawable);
            }
            ta.recycle();
        }
    }

    public void setVipImageResource(int resId) {
        mIv_vip.setVisibility(View.VISIBLE);
        mIv_vip.setImageResource(resId);
    }

    public void setVipImageGone(int resId) {
        mIv_vip.setVisibility(View.GONE);
    }

    public void setLevelResource(int resId) {
        mIv_level.setVisibility(View.VISIBLE);
        mIv_level.setImageResource(resId);
    }

    public void setLevelGone(int resId) {
        mIv_level.setVisibility(View.GONE);
    }

    public void setVipBGImageResource(int resId) {
        iv_vip_bg.setImageResource(resId);
    }

    public void setUserHeadImageResource(int resId) {
        iv_user_head.setImageResource(resId);
    }

    public void setUserHeadImageResource(String url) {
        Glide.with(context).load(url).crossFade().error(R.mipmap.default_head).into(iv_user_head);
    }

    public void setUserNickNameText(String nickName) {
        tv_user_nickname.setText(nickName);
    }

    public void setVipDateText(String date) {
        tv_date.setText(date);
    }

    public void setLeftLayoutClickListener(OnClickListener listener) {
        ll_back.setOnClickListener(listener);
    }

    public ImageView getHeadImageView() {
        return iv_user_head;
    }
}
